# from model import predict
from const import IMG_SIZE
import cv2
import numpy as np
from bmp_util import sort_pobjs, sort_pobjs_num
from path_object import PathObject, get_path_objects

DIVIDE = 1
ADD_MULT_SUB = 0


def eq_part_split(pos, line_thres_rate=0.5):
    pos.sort(key=lambda y: y.min_y + (y.max_y - y.min_y)/2)
    tmp_all_po = PathObject()
    for p in pos:
        tmp_all_po.add_paths(p)
    line_thres = (tmp_all_po.max_x - tmp_all_po.min_x) * line_thres_rate

    part_pos = []
    line_pos = []
    tmp_part_pos = []
    last_mid = 0
    last_y = 0

    for p in pos:
        if (p.min_y+(p.max_y-p.min_y)/2 > last_y and last_mid < p.min_y and len(tmp_part_pos) > 0) or p.max_x - p.min_x > line_thres:
        # if (p.min_y > last_y and len(tmp_part_pos) > 0) or p.max_x - p.min_x > line_thres:
            part_pos.append(tmp_part_pos)
            tmp_part_pos = []
        last_y = max(p.max_y, last_y)
        last_mid = max(p.min_y+(p.max_y-p.min_y)/2, last_mid)
        if p.max_x - p.min_x > line_thres:
            print("here")
            part_pos.append(None)
            line_pos.append(p)
        else:
            tmp_part_pos.append(p)
    if len(tmp_part_pos) > 0:
        part_pos.append(tmp_part_pos)

    return part_pos, line_pos, tmp_all_po


def mnist_eq(part_pos, line_pos, type):
    strs = []
    for i, ppos in enumerate(part_pos):
        if ppos is None:
            strs.append("_")
            continue
        if len(ppos) == 0:
            continue
        ppos.sort(key=lambda x: x.min_x)
        if type == DIVIDE and i == 2:
            lo = line_pos[0]
            dline_index = -1
            for j, po in enumerate(ppos):
                if po.min_x > lo.min_x:
                    dline_index = j - 1
                    break
            ppos1 = ppos[:dline_index]
            ppos2 = ppos[dline_index + 1:]
            s, ppos1 = sort_pobjs_num(ppos1)
            s += "/"
            s2, ppos2 = sort_pobjs_num(ppos2)
            s += s2
            strs.append(s)
            part_pos[i] = ppos1+[ppos[dline_index]] +ppos2 #[ppos1, ppos2]
        else:
            s, ppos = sort_pobjs_num(ppos)
            strs.append(s)
            part_pos[i] = ppos
    return strs, part_pos


def format_print(strs, part_pos, type):
    max_len = 0
    max_index = 0
    max_ppos = []
    for i, ppos in enumerate(part_pos):
        if ppos is None:
            continue
        if len(ppos) > max_len:
            max_len = len(ppos)
            max_index = i
            max_ppos = ppos
    format_strs = []
    print(str(max_index) + " : " + strs[max_index])
    dividesign_index = 0
    for i, ppos in enumerate(part_pos):
        if i == max_index:
            format_strs.append(strs[max_index])
            continue
        if strs[i] == "_":
            if type == DIVIDE:
                if i == 1:
                    for i2, c in enumerate(strs[2]):
                        if c == "/":
                            dividesign_index = i2
                            tmp_s = (i2 + 1) * " " + (len(strs[2]) - i2 - 1) * "_"
                            break
                else:
                    tmp_s = (dividesign_index + 1) * " " + (len(strs[2]) - dividesign_index - 1) * "_"
            else:
                tmp_s = max_len * "_"
            format_strs.append(tmp_s)
            continue

        tmp_s = max_len * " "
        k = 0

        for j, po in enumerate(ppos):
            aligned = False
            for k0, mpo in enumerate(max_ppos[k:]):
                offset = 0
                offset = (min(po.max_y-po.min_y, mpo.max_y-mpo.min_y))/4
                mid = po.min_x + (po.max_x - po.min_x + 20) / 2
                mid2 = mpo.min_x + (mpo.max_x - mpo.min_x + 20) / 2
                if mpo.max_x + offset > mid > mpo.min_x - offset \
                        or po.max_x +offset> mid2 > po.min_x - offset:
                    k += k0
                    tmp_s = tmp_s[:k] + strs[i][j] + tmp_s[k + 1:]
                    k += 1
                    aligned = True
                    break
            if not aligned:
                tmp_s = tmp_s[:k] + strs[i][j] + tmp_s[k + 1:]
                k += 1
        format_strs.append(tmp_s)
    for e, fs in enumerate(format_strs):
        fs = str.replace(fs, "D", "0")
        fs = str.replace(fs, ">", "×")
        fs = str.replace(fs, "<", "×")
        fs = str.replace(fs, "×", "x")
        # print(fs)
        format_strs[e] = fs
    return format_strs


def vert_equal_mnist(paths):
    pos = get_path_objects(paths)
    part_pos, line_pos, tmp_all_po = eq_part_split(pos)

    for e, ppos in enumerate(part_pos):
        if ppos is None:
            continue
        if len(ppos) == 1:
            tmp_s, _ = sort_pobjs_num(ppos)
            if tmp_s == "-":
                part_pos[e] = None

    if part_pos[1] is None:
        # print("除法")
        eq_type = DIVIDE
    else:
        # print("加减乘法")
        eq_type = ADD_MULT_SUB
    strs, part_pos = mnist_eq(part_pos, line_pos, eq_type)
    strs = format_print(strs, part_pos, eq_type)
    return strs, eq_type, tmp_all_po


if __name__ == "__main__":
    #23+45=68
    path1 = [[[602,103],[620,103],[632,106],[646,108],[656,117],[664,127],[664,141],[658,162],[644,187],[626,202],[610,216],[603,220],[598,220],[598,223],[606,220],[625,217],[648,213],[668,209],[687,208],[701,207],[712,207],[712,207]],[[833,60],[857,60],[866,60],[874,60],[880,60],[881,62],[879,70],[863,88],[845,104],[832,113],[824,120],[821,122],[820,122],[822,122],[835,120],[854,120],[877,123],[900,129],[909,140],[910,152],[902,165],[874,181],[840,195],[809,200],[786,205],[772,205],[766,205],[764,205],[761,208],[761,208]],[[371,371],[392,371],[403,371],[417,370],[428,370],[438,370],[449,370],[457,369],[457,369]],[[429,304],[429,330],[430,350],[431,371],[431,391],[431,406],[431,417],[433,425],[432,428],[432,431],[432,431]],[[629,300],[627,323],[623,356],[623,371],[622,379],[622,387],[622,387],[626,389],[634,390],[651,387],[673,384],[691,379],[707,375],[718,371],[726,369],[730,369],[730,369]],[[689,303],[686,325],[683,361],[679,395],[676,420],[671,440],[669,447],[669,447],[669,447]],[[857,299],[855,310],[856,326],[857,334],[863,341],[870,348],[878,354],[886,358],[894,360],[900,364],[906,368],[907,372],[907,379],[897,394],[886,404],[868,416],[854,422],[839,424],[829,424],[829,424]],[[837,319],[861,316],[878,315],[894,314],[907,311],[907,311]],[[348,500],[362,504],[385,501],[412,496],[447,490],[492,481],[533,476],[574,470],[614,465],[661,462],[705,463],[751,466],[795,468],[839,471],[881,473],[919,473],[952,469],[981,466],[1007,461],[1026,458],[1042,458],[1048,460],[1048,460],[1048,460]],[[676,504],[667,522],[656,553],[648,577],[639,600],[631,621],[627,639],[624,653],[624,666],[627,673],[630,675],[639,679],[651,681],[668,680],[690,675],[708,670],[721,664],[729,658],[729,653],[726,650],[716,644],[698,641],[683,637],[668,636],[657,636],[650,634],[646,634],[646,634]],[[907,589],[912,577],[916,560],[918,542],[918,523],[914,507],[905,495],[894,492],[883,494],[868,502],[858,512],[850,522],[848,535],[850,545],[858,558],[873,570],[894,582],[915,593],[931,605],[941,615],[941,626],[932,637],[914,654],[892,665],[872,673],[862,674],[858,674],[859,670],[865,654],[880,630],[902,601],[925,580],[938,569],[938,569]]]

    #12*12=144
    path2 = [[[1044,39],[1041,67],[1037,84],[1033,101],[1030,116],[1028,129],[1027,137],[1027,141],[1027,141]],[[1112,55],[1123,53],[1138,52],[1144,53],[1146,64],[1137,86],[1124,108],[1114,123],[1109,132],[1105,137],[1104,140],[1105,141],[1107,139],[1123,133],[1138,127],[1155,122],[1166,119],[1176,117],[1183,117],[1186,117],[1186,117]],[[628,221],[645,236],[654,247],[664,257],[673,268],[682,276],[689,285],[696,292],[699,296],[699,296]],[[707,215],[695,225],[678,242],[662,256],[652,264],[646,270],[643,272],[642,275],[642,277],[642,277]],[[1047,211],[1046,233],[1043,250],[1044,267],[1043,277],[1043,285],[1043,290],[1043,291],[1043,291]],[[1109,215],[1121,215],[1136,216],[1142,219],[1146,225],[1144,237],[1137,253],[1130,268],[1124,281],[1121,288],[1121,294],[1121,296],[1122,298],[1127,298],[1143,292],[1159,287],[1170,282],[1182,278],[1190,278],[1195,280],[1195,280]],[[618,384],[631,389],[661,391],[696,392],[736,392],[779,391],[820,390],[863,390],[906,390],[949,389],[984,386],[1022,383],[1059,376],[1090,373],[1118,371],[1146,371],[1169,371],[1188,371],[1200,368],[1217,365],[1233,364],[1243,363],[1254,363],[1264,362],[1271,360],[1276,359],[1276,359]],[[1013,463],[1023,455],[1036,454],[1042,456],[1048,465],[1052,475],[1048,488],[1045,503],[1032,522],[1021,535],[1013,544],[1010,547],[1009,549],[1018,546],[1032,544],[1050,541],[1069,537],[1086,533],[1100,528],[1109,524],[1115,521],[1115,521]],[[1163,403],[1158,429],[1157,441],[1157,453],[1158,460],[1161,470],[1168,480],[1172,489],[1179,497],[1185,503],[1190,505],[1196,505],[1200,500],[1212,493],[1221,487],[1232,481],[1238,475],[1241,472],[1241,472]],[[1240,399],[1229,425],[1212,460],[1194,490],[1182,521],[1173,539],[1168,551],[1166,558],[1164,560],[1164,560]],[[1014,605],[1025,611],[1044,616],[1051,621],[1055,624],[1057,634],[1055,642],[1046,656],[1035,668],[1025,677],[1016,683],[1011,686],[1009,688],[1006,689],[1005,689],[1008,689],[1010,689],[1018,689],[1027,689],[1040,688],[1052,686],[1062,685],[1069,682],[1073,682],[1077,682],[1077,682]],[[885,590],[890,612],[891,626],[891,643],[889,656],[888,666],[885,676],[885,685],[884,693],[884,698],[884,698]],[[641,724],[665,731],[682,732],[709,735],[743,735],[777,735],[814,734],[847,734],[880,732],[917,730],[956,729],[994,729],[1030,730],[1063,733],[1089,734],[1114,736],[1133,736],[1148,736],[1171,736],[1193,734],[1211,735],[1227,734],[1242,734],[1255,732],[1266,732],[1277,731],[1286,731],[1289,731],[1295,731],[1299,731],[1303,731],[1308,731],[1311,731],[1312,731],[1314,731],[1316,731],[1317,731],[1317,731]],[[900,775],[898,792],[892,818],[888,835],[883,851],[879,858],[879,863],[876,867],[876,867]],[[1010,776],[999,803],[998,814],[996,823],[996,830],[997,834],[999,837],[1007,841],[1017,842],[1034,840],[1055,832],[1074,825],[1091,820],[1103,818],[1110,816],[1110,816]],[[1077,764],[1066,794],[1058,816],[1052,834],[1047,850],[1043,858],[1043,866],[1043,866]],[[1191,775],[1187,789],[1184,798],[1183,809],[1183,815],[1183,822],[1183,827],[1186,833],[1188,837],[1191,841],[1197,842],[1204,842],[1217,840],[1230,835],[1243,831],[1254,826],[1265,822],[1270,818],[1272,817],[1272,817]],[[1252,756],[1247,771],[1238,799],[1229,825],[1222,848],[1216,864],[1212,878],[1212,882],[1212,882]]]

    #111*101=11211
    path3 = [[[962,33],[962,49],[962,61],[961,79],[958,104],[953,132],[947,155],[942,171],[940,181],[937,188],[937,189],[937,189],[937,189]],[[1070,42],[1068,56],[1065,74],[1062,93],[1057,115],[1056,134],[1051,149],[1046,161],[1046,166],[1046,166]],[[1172,39],[1167,61],[1162,83],[1157,105],[1153,126],[1150,140],[1148,150],[1148,156],[1148,159],[1148,160],[1148,160]],[[546,269],[556,278],[570,287],[581,297],[592,307],[605,319],[615,332],[622,343],[627,354],[632,360],[632,360],[632,360]],[[618,264],[591,289],[564,312],[542,334],[525,353],[512,365],[504,372],[502,374],[499,374],[499,374]],[[946,251],[938,280],[933,300],[929,319],[928,331],[927,339],[926,343],[926,347],[926,348],[926,348],[926,348]],[[1029,263],[1023,274],[1017,285],[1014,294],[1013,302],[1013,308],[1014,313],[1021,321],[1031,328],[1043,330],[1054,331],[1062,330],[1065,324],[1066,318],[1062,307],[1056,297],[1048,288],[1043,283],[1037,279],[1033,276],[1033,274],[1031,273],[1031,273]],[[1140,223],[1137,261],[1131,293],[1129,325],[1130,347],[1132,362],[1134,375],[1134,375]],[[447,468],[457,471],[477,471],[504,471],[533,468],[567,467],[603,465],[636,463],[675,462],[718,460],[760,456],[805,450],[848,446],[888,442],[934,436],[979,430],[1023,424],[1067,419],[1110,415],[1144,413],[1175,411],[1215,406],[1248,402],[1279,399],[1306,396],[1328,393],[1345,391],[1359,390],[1372,389],[1379,389],[1385,389],[1385,389]],[[936,454],[918,487],[911,507],[906,523],[904,534],[903,541],[902,544],[902,547],[903,548],[905,549],[905,549]],[[1037,472],[1032,495],[1029,513],[1027,531],[1024,544],[1023,553],[1023,556],[1022,561],[1022,561],[1022,561]],[[1162,445],[1157,467],[1148,494],[1142,519],[1136,544],[1132,562],[1128,572],[1128,572]],[[926,623],[921,638],[917,655],[914,669],[913,683],[911,692],[909,700],[908,704],[907,708],[905,709],[905,709],[905,709]],[[794,630],[789,652],[785,674],[780,694],[777,710],[775,720],[772,724],[772,724],[772,724]],[[670,597],[659,636],[652,663],[644,691],[642,712],[641,723],[640,730],[640,733],[640,733]],[[442,801],[468,800],[492,797],[527,793],[562,787],[601,783],[642,778],[679,774],[727,770],[770,767],[821,762],[866,760],[909,758],[950,758],[992,756],[1040,753],[1084,751],[1123,748],[1168,747],[1205,745],[1245,743],[1281,742],[1311,742],[1341,741],[1368,741],[1392,741],[1413,741],[1431,741],[1445,742],[1457,743],[1464,743],[1468,743],[1470,743],[1472,743],[1471,743],[1471,743]],[[662,824],[655,843],[649,855],[645,865],[641,878],[639,889],[637,898],[636,904],[634,907],[634,910],[634,910]],[[776,826],[766,853],[764,865],[760,881],[761,897],[761,898],[761,898]],[[904,817],[918,814],[929,813],[937,813],[943,813],[947,818],[945,825],[939,837],[930,849],[920,858],[911,865],[904,873],[898,879],[896,883],[898,883],[908,883],[920,880],[934,878],[946,878],[957,875],[964,874],[970,874],[970,874]],[[1078,792],[1076,810],[1071,826],[1068,842],[1068,855],[1068,869],[1068,870],[1068,870]],[[1169,807],[1168,824],[1166,841],[1164,857],[1161,871],[1159,891],[1159,891]]]

    #94/2=47
    path4 = [[[746,155],[765,157],[807,157],[855,156],[902,150],[945,146],[981,143],[1014,145],[1044,147],[1062,155],[1078,161],[1083,161],[1087,161],[1088,161],[1088,161]],[[742,162],[737,184],[730,207],[719,240],[707,272],[695,300],[688,324],[680,339],[679,343],[678,347],[678,347]],[[869,227],[857,216],[848,209],[836,205],[824,203],[813,204],[800,211],[790,218],[784,226],[784,236],[785,243],[795,250],[808,252],[827,250],[847,243],[856,237],[864,236],[867,235],[869,236],[869,237],[865,247],[856,265],[847,288],[839,306],[833,322],[830,335],[828,343],[828,348],[828,348],[828,348]],[[996,205],[988,225],[985,236],[984,247],[984,254],[986,260],[994,268],[1007,275],[1024,279],[1037,279],[1051,275],[1062,271],[1071,266],[1071,266]],[[1052,211],[1039,240],[1029,263],[1018,284],[1010,301],[1002,317],[996,330],[993,334],[993,334]],[[545,218],[563,216],[578,216],[593,218],[604,220],[613,226],[616,231],[611,242],[592,264],[574,279],[564,289],[558,293],[557,295],[568,294],[585,291],[602,289],[615,288],[626,287],[634,287],[635,287],[635,287]],[[808,4],[799,22],[794,36],[790,49],[787,60],[785,68],[784,76],[784,81],[786,82],[793,83],[810,83],[831,81],[851,79],[865,75],[874,74],[874,74]],[[843,31],[829,73],[812,114],[800,146],[793,168],[792,177],[791,183],[791,183]],[[804,457],[812,446],[821,434],[824,424],[827,416],[827,409],[826,404],[819,398],[810,393],[801,387],[793,384],[789,383],[785,383],[784,383],[784,386],[784,393],[784,409],[786,428],[791,442],[800,453],[810,461],[818,466],[829,472],[836,479],[841,483],[843,487],[841,488],[836,490],[825,495],[814,496],[802,496],[793,495],[787,493],[783,485],[783,481],[786,473],[794,464],[808,457],[821,451],[829,448],[829,448]],[[709,529],[735,529],[770,526],[825,521],[886,515],[952,506],[1013,497],[1061,489],[1091,485],[1106,484],[1108,484],[1110,484],[1110,484]],[[845,543],[839,570],[833,595],[830,616],[828,630],[826,639],[826,643],[824,645],[824,648],[824,648]],[[955,540],[950,560],[948,573],[948,586],[950,599],[952,607],[958,612],[968,612],[988,611],[1007,606],[1028,602],[1042,599],[1051,597],[1059,595],[1059,595]],[[1030,535],[1011,575],[1005,600],[999,617],[999,626],[998,631],[996,633],[996,633]],[[979,22],[1007,30],[1023,31],[1033,33],[1043,34],[1050,37],[1054,40],[1056,42],[1053,46],[1039,66],[1024,83],[1014,97],[1007,106],[1004,117],[1004,117]],[[851,676],[846,693],[840,713],[833,735],[830,750],[827,759],[826,764],[826,767],[828,767],[828,767]],[[937,669],[936,681],[942,694],[952,701],[965,709],[982,713],[1000,714],[1012,713],[1023,713],[1029,710],[1035,708],[1038,707],[1038,707]],[[1015,670],[1009,682],[995,717],[986,743],[980,760],[978,767],[976,770],[976,770]],[[674,813],[700,813],[727,813],[762,813],[798,813],[841,810],[883,806],[926,802],[968,800],[1007,800],[1039,800],[1068,798],[1089,796],[1102,795],[1109,795],[1111,793],[1112,793],[1112,793]],[[972,844],[967,855],[966,867],[966,877],[969,892],[971,906],[976,915],[982,919],[989,920],[993,918],[1000,915],[1004,908],[1005,904],[1003,892],[992,878],[978,865],[967,858],[957,854],[953,851],[949,851],[949,851]]]

    #113*103=11639
    path5 = [[[486,193],[487,245],[486,287],[490,360],[494,382]],[[630,244],[631,270],[629,302],[626,336],[625,356]],[[760,205],[779,206],[788,210],[793,218],[772,258],[744,286],[728,303],[721,311],[731,310],[773,310],[793,313],[807,313],[816,316],[819,320],[795,346],[774,364],[758,380]],[[494,510],[487,559],[481,598],[475,647]],[[632,537],[628,548],[621,584],[620,610],[628,638],[633,637],[662,598],[668,575],[668,549],[663,545],[654,543],[652,543],[650,543]],[[743,510],[764,519],[778,527],[790,534],[796,547],[790,554],[779,568],[770,579],[763,587],[761,591],[771,596],[789,598],[807,601],[822,604],[828,618],[811,635],[790,651],[755,680]],[[103,567],[128,581],[156,593],[185,604],[205,614]],[[208,547],[176,592],[145,636],[135,648],[124,657]],[[84,801],[97,792],[114,785],[154,777],[178,775],[205,774],[261,773],[297,771],[336,769],[373,768],[415,767],[453,767],[488,766],[527,765],[568,762],[611,758],[652,757],[697,758],[772,761],[808,761],[842,763],[874,766],[903,767],[933,770],[959,771],[982,772],[999,771],[1013,768]],[[450,852],[463,848],[484,842],[510,840],[515,842],[504,858],[488,872],[475,886],[466,893],[460,897],[457,898],[469,898],[486,898],[503,902],[517,906],[524,911],[524,918],[500,937],[485,945],[476,950],[469,954]],[[660,823],[680,830],[691,839],[688,852],[671,877],[639,911],[631,917],[632,920],[633,920],[664,916],[680,917],[691,920],[686,939],[665,959],[649,972],[638,981],[633,985]],[[867,845],[860,823],[852,817],[836,819],[793,844],[784,855],[782,865],[786,871],[798,869],[823,862],[865,842],[875,837],[883,837],[883,838],[876,853],[860,894],[836,964],[828,983],[823,999],[820,1012],[818,1024],[817,1033]],[[452,1028],[466,1028],[478,1030],[487,1036],[485,1042],[462,1074],[454,1082],[451,1086],[454,1088],[491,1085],[515,1088],[529,1089],[534,1093],[528,1097],[511,1108],[472,1126],[459,1133]],[[355,1022],[347,1040],[349,1057],[354,1065],[361,1073],[376,1074],[381,1067],[384,1056],[384,1037],[382,1034],[380,1030],[372,1026],[371,1025],[368,1022]],[[229,987],[232,1024],[232,1055],[232,1087],[229,1131]],[[180,1171],[212,1171],[285,1167],[330,1165],[371,1162],[439,1166],[463,1167],[486,1170],[520,1171],[551,1173],[586,1175],[619,1177],[680,1178],[705,1178],[737,1176],[801,1173],[832,1173],[860,1173],[903,1176],[918,1176],[965,1180],[991,1181],[1006,1180]],[[266,1219],[270,1251],[270,1281],[270,1312],[265,1349],[263,1357],[263,1359]],[[366,1263],[369,1278],[381,1296],[387,1301],[401,1303],[412,1294],[423,1270],[421,1263],[416,1255],[414,1253],[409,1250],[408,1248]],[[530,1207],[525,1236],[525,1265],[525,1289],[526,1324],[530,1333],[539,1347],[546,1350],[566,1343],[577,1332],[586,1315],[584,1311],[564,1308],[551,1310],[536,1311],[534,1311]],[[691,1260],[709,1260],[719,1262],[725,1263],[728,1272],[723,1281],[717,1289],[713,1295],[712,1299],[714,1305],[723,1311],[734,1322],[744,1337],[744,1358],[737,1367],[726,1373],[718,1378],[712,1380],[712,1380]],[[922,1295],[910,1284],[894,1265],[878,1247],[853,1227],[844,1224],[837,1227],[835,1230],[833,1238],[833,1243],[836,1256],[838,1261],[845,1266],[850,1268],[858,1268],[862,1268],[868,1268],[870,1268],[868,1311],[863,1346],[855,1392],[852,1403],[850,1409]]]

    #8*12=96
    path6 = [[[722,257],[722,199],[706,143],[689,122],[653,100],[640,100],[622,122],[615,144],[620,201],[642,238],[697,291],[726,314],[758,358],[757,375],[734,392],[707,395],[648,392],[625,386],[606,371],[623,343],[659,297],[702,256],[745,229],[780,212]],[[266,522],[276,527],[294,536],[323,550],[374,586],[389,598],[396,601],[397,601]],[[328,500],[318,523],[300,559],[279,590],[249,638],[242,650]],[[568,485],[566,505],[558,545],[548,577],[533,623],[525,641]],[[707,501],[722,504],[735,506],[746,510],[760,521],[762,530],[739,573],[716,597],[689,619],[684,623],[684,624],[686,624],[717,623],[740,623],[764,622],[776,620]],[[274,739],[284,742],[361,743],[435,738],[591,720],[668,715],[803,713],[858,712],[906,710],[965,704]],[[760,801],[748,814],[726,841],[707,871],[692,897],[679,923],[670,966],[673,982],[675,987],[683,993],[699,992],[747,968],[767,949],[785,929],[794,911],[793,899],[786,897],[764,896],[743,897],[721,901]],[[574,831],[573,854],[568,889],[562,920],[552,966]],[[586,1177],[599,1152],[614,1119],[625,1087],[633,1048],[632,1039],[630,1037],[612,1035],[599,1035],[584,1038],[570,1044],[560,1049],[552,1056],[547,1063],[546,1072],[553,1082],[571,1097],[592,1114],[621,1148],[628,1170],[628,1187],[625,1201],[607,1221],[592,1225],[578,1229],[561,1228],[549,1225],[539,1219],[535,1210],[536,1198],[558,1160],[577,1147],[591,1135],[595,1133]],[[314,1292],[332,1297],[357,1301],[400,1301],[513,1292],[576,1285],[710,1267],[782,1258],[927,1242],[979,1238],[983,1238]],[[692,1412],[681,1404],[659,1389],[642,1380],[623,1374],[588,1369],[574,1371],[562,1379],[558,1388],[554,1406],[554,1410],[563,1414],[581,1407],[605,1394],[633,1378],[670,1357],[682,1354],[685,1355],[685,1359],[673,1408],[658,1441],[646,1471],[633,1496],[622,1519],[613,1542]],[[810,1326],[800,1350],[791,1380],[782,1413],[770,1471],[771,1492],[777,1513],[786,1523],[811,1527],[826,1521],[852,1501],[860,1489],[857,1467],[843,1457],[809,1450],[793,1450],[773,1447],[771,1447]]]

    path7 = [[[701,196],[717,198],[751,210],[762,219],[767,242],[762,253],[751,264],[726,277],[717,281],[706,282],[710,282],[715,283],[722,284],[728,290],[726,298],[717,316],[704,335],[689,348],[672,361],[657,370],[642,377],[633,384]],[[381,514],[411,539],[433,558],[454,577],[483,595],[489,596]],[[454,482],[430,530],[415,568],[399,603],[374,659]],[[652,504],[668,504],[684,508],[702,515],[714,522],[721,539],[715,550],[707,563],[697,576],[686,587],[679,597],[674,602],[673,606],[673,607],[682,605],[709,600],[735,596],[759,594],[779,594],[791,595],[798,597]],[[399,718],[436,718],[483,722],[536,726],[592,729],[648,731],[701,732],[749,730],[791,727],[824,723],[849,724],[864,728],[869,730]],[[726,714],[708,741],[687,812],[681,851],[678,884],[678,909],[681,927],[689,939],[707,949],[731,953],[755,949],[776,937],[792,921],[795,888],[785,882],[766,879],[723,888],[708,896],[698,903],[698,903]]]

    path8 = [[[14,462]],[[573,163],[569,192],[567,222],[566,250],[566,286],[565,298],[564,305],[566,310]],[[698,170],[718,172],[725,176],[734,183],[729,195],[718,220],[704,242],[694,259],[688,269],[687,274],[691,275],[707,274],[727,272],[750,266],[772,262],[785,260],[797,260],[805,260],[808,261],[810,261]],[[567,419],[562,438],[556,482],[553,503],[551,529],[551,536],[551,539]],[[716,409],[730,409],[743,411],[757,414],[772,426],[772,436],[764,454],[750,476],[736,492],[723,506],[713,515],[709,521],[707,525],[709,525],[716,525],[736,523],[760,519],[782,517],[801,517],[814,518]],[[287,418],[301,429],[329,454],[355,480],[392,524],[405,539],[411,551],[411,552]],[[399,432],[377,464],[358,492],[340,517],[315,551],[315,551]],[[219,628],[244,620],[293,609],[342,605],[403,600],[535,591],[603,589],[677,585],[743,582],[810,581],[870,581],[923,581],[965,582],[999,585],[1000,585]],[[505,706],[519,704],[538,701],[563,704],[570,708],[578,728],[571,754],[557,785],[540,811],[528,828],[518,838],[515,841],[515,842],[526,838],[549,831],[574,821],[598,815],[620,810],[632,808]],[[718,708],[712,730],[712,745],[712,763],[724,793],[734,801],[764,803],[788,792],[793,789]],[[786,705],[775,725],[759,763],[747,803],[723,867]],[[513,911],[531,892],[545,886],[562,883],[593,884],[600,892],[580,931],[558,958],[518,991],[508,998],[504,1002],[510,1002],[528,996],[548,989],[567,983],[589,977],[594,980]],[[298,891],[299,929],[305,992],[305,1016],[304,1038]],[[136,1117],[153,1112],[205,1101],[263,1091],[398,1076],[475,1071],[557,1064],[643,1058],[728,1052],[878,1047],[945,1045],[991,1043],[1023,1045],[1041,1046]],[[305,1223],[305,1258],[306,1289],[306,1314],[306,1338],[306,1340]],[[450,1154],[444,1179],[441,1202],[438,1224],[447,1248],[460,1255],[510,1248],[539,1237],[582,1219]],[[525,1180],[520,1226],[522,1255],[524,1283],[529,1306],[536,1320]],[[728,1141],[708,1180],[706,1203],[707,1222],[731,1239],[751,1242],[802,1231]],[[781,1160],[767,1209],[759,1249],[754,1288],[753,1316]]]

    path9 = [[[463,282],[504,281],[597,275],[656,265],[763,239],[801,231],[813,229]],[[480,279],[470,325],[458,369],[443,410],[413,475],[405,496],[400,505]],[[534,358],[530,377],[520,419],[512,452],[507,487],[507,492],[508,490]],[[596,381],[591,395],[589,409],[589,421],[595,435],[610,442],[632,446]],[[649,356],[636,400],[627,437],[620,473],[614,510],[614,513]],[[712,349],[701,377],[696,396],[695,412],[696,431],[703,441],[729,447],[760,438],[806,414],[819,404],[828,399]],[[801,346],[780,406],[762,490],[758,512],[755,528]],[[237,347],[231,369],[224,399],[219,428],[217,471],[217,478],[217,479]],[[315,334],[331,341],[339,344],[347,348],[353,360],[349,379],[331,419],[324,430],[321,439],[322,441],[329,442],[339,444],[358,447],[363,448],[365,448]],[[658,84],[654,94],[644,137],[636,182],[621,252]],[[523,556],[507,602],[493,651],[491,665],[488,670]],[[562,557],[584,562],[599,567],[610,574],[616,594],[610,614],[593,645],[589,653],[586,662],[589,661],[609,663],[623,665],[637,665],[647,665],[656,665]],[[442,755],[459,751],[493,748],[541,744],[657,737],[718,733],[835,720],[884,717],[946,718]],[[599,810],[624,814],[631,840],[626,865],[609,905],[602,917],[601,925],[619,925],[650,917],[682,909],[708,903]],[[738,790],[738,813],[742,830],[757,851],[771,858],[789,861],[828,859],[846,854]],[[822,775],[810,821],[803,868],[799,906],[796,944],[796,950]],[[753,95],[768,95],[775,98],[771,117],[751,157],[743,169],[736,182],[735,185],[737,185],[748,181],[794,168],[815,162],[843,158],[849,157],[856,157]],[[601,1022],[614,1019],[632,1018],[639,1019],[649,1027],[648,1038],[634,1069],[625,1083],[608,1101],[604,1107],[601,1112],[607,1112],[634,1110],[652,1108],[679,1103],[687,1100],[695,1098],[696,1097],[697,1096]],[[781,979],[775,997],[774,1027],[774,1041],[777,1059],[780,1061],[800,1065],[816,1066],[851,1060],[860,1055]],[[819,999],[815,1027],[812,1085],[808,1121],[806,1149]],[[490,1206],[512,1209],[599,1217],[655,1221],[760,1215],[815,1210],[901,1202],[932,1205],[942,1206]],[[797,1254],[782,1281],[782,1303],[783,1328],[794,1365],[803,1375],[831,1372],[849,1357],[873,1324],[878,1306],[876,1283],[867,1274],[839,1264],[823,1263],[801,1259]]]

    strs, eq_type, po = vert_equal_mnist(path9)
    for s in strs:
        print(s)

    im = po.draw(po.max_x, po.max_y, thickness=6)
    im = cv2.resize(im, (int(im.shape[1]/2), int(im.shape[0]/2)))
    cv2.imshow("im", im)
    cv2.waitKey()